<template>
	<view :class="['page',{'iphoneX':isIphoneX}]">
		<view v-if="isLogin" class="body">
			<!-- <view class="banner m-b2">
				<image src="@/static/images/home_banner.png" mode="widthFix"></image>
				<navigator url="/pages/msg/msg" open-type="navigate" hover-class="none" class="notice iconfont" :style="{'top':(systemInfo.safeArea.top+10)+'px'}">
					<view class="f-s40">&#xe654;</view>
					<view v-if="new_msg_num>0" class="num">
						<text v-if="new_msg_num<=99">{{new_msg_num}}</text>
						<text v-if="new_msg_num>99">99+</text>
					</view>
				</navigator>
				<navigator url="/pages/setting/setting" hover-class="none" class="iconfont setting f-s48" :style="{'top':(systemInfo.safeArea.top+9)+'px'}">&#xe70f;</navigator>
			</view> -->
			<view v-if="userInfo.username" class="welcome-text">
				<view>您好，{{userInfo.username}}</view>
				<navigator url="/pages/msg/msg" open-type="navigate" hover-class="none" class="notice iconfont" :style="{'top':(systemInfo.safeArea.top+10)+'px'}">
					<view class="f-s40">&#xe654;</view>
					<view v-if="new_msg_num>0" class="num">
						<text v-if="new_msg_num<=99">{{new_msg_num}}</text>
						<text v-if="new_msg_num>99">99+</text>
					</view>
				</navigator>
			</view>
			<view class="project-info-data m-b2">
				<template v-if="userInfo.role_id==11||userInfo.role_id==1">
					<navigator url="/pages/project/project" open-type="reLaunch" hover-class="none" class="item p-2">
						<view class="icon"><image src="@/static/images/dsp_icon.png" mode="widthFix"></image></view>
						<view class="data-info">
							<view class="text">所有项目</view>
							<view class="val">{{num_data.all_project}}</view>
						</view>					
					</navigator>
					<navigator url="/pages/project/project?status=3" open-type="reLaunch" hover-class="none" class="item pass">
						<view class="icon"><image src="@/static/images/new_task_icon.png" mode="widthFix"></image></view>
						<view class="data-info">
							<view class="text">进行中项目</view>
							<view class="val">{{num_data.implement}}</view>
						</view>
					</navigator>
					<!-- <navigator url="/pages/project/project?warn_level=1" open-type="reLaunch" hover-class="none" class="item red">
						<view class="icon"><image src="@/static/images/yq_icon.png" mode="widthFix"></image></view>
						<view class="data-info">
							<view class="text">项目逾期</view>
							<view class="val f-red">{{num_data.overdue}}</view>
						</view>
					</navigator> -->
				</template>
				<navigator url="/package_msg/msg-list/msg-list?type=0&is_read=0" hover-class="none" class="item p-2"> 
					<view class="icon"><image src="@/static/images/cl_icon.png" mode="widthFix"></image></view>
					<view class="data-info">
						<view class="text">待处理</view>
						<view class="val">{{num_data.approver_info}}</view>
					</view>
				</navigator>
				<navigator url="/package_msg/msg-list/msg-list?type=3&is_read=0" hover-class="none" class="item p-2"> 
					<view class="icon"><image src="@/static/images/xrw_icon.png" mode="widthFix"></image></view>
					<view class="data-info">
						<view class="text">新任务</view>
						<view class="val">{{num_data.task_info}}</view>
					</view>					
				</navigator>
				
			</view>
			
			<view class="mod-grid m-b2">
				<view class="hd-title">
					<view class="title">最新项目</view>
					<navigator url="/pages/project/project" open-type="reLaunch" hover-class="none" class="more">全部 ></navigator>
				</view>
				<view class="project-list">
					<c-loading v-if="loadingState.default" :loadingState="loadingState"></c-loading>
					<template v-else>
						<template v-if="pro_list.length>0">
						<navigator :url="'/package_project/project-detail/project-detail?id='+item.id" hover-class="none" class="item m-b2" v-for="(item,index) in pro_list" :key="index">
							<view class="hd">
								<view class="title">
									<view class="name">{{item.project_name}}</view>
									<view class="status">
										<text v-if="item.status==0" class="blue">项目立项</text>
										<text v-if="item.status==1" class="blue">审批中</text>
										<text v-if="item.status==2" class="blue">审批完成</text>
										<text v-if="item.status==3" class="blue">实施中</text>
										<text v-if="item.status==4" class="blue">报告撰写</text>
										<text v-if="item.status==5" class="blue">验收中</text>
										<text v-if="item.status==6" class="blue">已完成</text>
										<text v-if="item.status==7" class="blue">审批不通过</text>
										<!-- <text class="blue">
											<text v-if="detail.status==0" class="btn orange">项目立项</text>
											<text v-if="detail.status==1" class="btn orange">审批中</text>
											<text v-if="detail.status==2" class="btn orange">审批完成</text>
											<text v-if="detail.status==3" class="btn orange">实施中</text>
											<text v-if="detail.status==4" class="btn orange">报告撰写</text>
											<text v-if="detail.status==5" class="btn orange">验收中</text>
											<text v-if="detail.status==6" class="btn green">已完成</text>
											<text v-if="detail.status==7" class="btn red">审批不通过</text>
										</text> 	 -->	
									</view>
								</view>
								<view class="date">{{item.start_time}}~{{item.end_time}}</view>
							</view>
							<view class="content"> 
								<view class="s-item">
									<view class="label">项目经理：</view>
									<view class="val">{{item.responsible_name||'未指派'}}</view>
								</view>
								<view class="row-2">
									<view class="s-item">
										<view class="label">项目金额：</view>
										<view class="val f-red f-wb">{{item.amount}}</view>
									</view>
									<view class="s-item">
										<view class="label">回款金额：</view>
										<view class="val f-red f-wb">{{item.finish_monback}}</view>
									</view>
								</view> 	 
								<!-- <view class="finish-icon"><image src="@/static/images/gq_icon.png" mode="widthFix"></image></view> -->
							</view>						 
						</navigator>
						</template>
						
						<c-empty v-else></c-empty>
					 
					 </template>
				</view> 
			</view>
			 
		</view> 
	
		<c-nav-project :switchTab="4"></c-nav-project>
	</view>
</template>

<script> 
	export default {
		data() {
			return {
				isLogin:false, 
				userInfo:{},
				isIphoneX:getApp().globalData.isIphoneX,
				systemInfo:uni.getSystemInfoSync(),
				loadingState:{
					default:true,
					loading:false,
					complete:false
				},
				num_data:{},
				new_msg_num:0,
				pro_list:[]
			}
		},
		async onLoad() {
			this.isLogin=await this.$loginState('check');  
			if(this.isLogin){
				this.getHomeData();
				this.getMessageList();
				this.getList();
				this.getUser();
			}
		},
		async onShow() { 
			
			
		},
		methods: { 
			
			async getUser(){
				let res = await this.$api.user_api.Index(); 
				if (res.data.code == 200) {  
					this.userInfo=res.data.data;
				}
			},
			
			
			// 获取用户信息
			async getUserInfo(){
				let userInfo = await this.$api.user_api.Index();
				if (userInfo.data.code == 200) {
					this.userInfo=userInfo.data.data;	
					uni.setStorage({
						key:'userInfo',
						data:userInfo.data.data
					})
					// WebSocket.checkSocketState();   // 登录成功开启socket聊天
					
				}
			},
			
			async getHomeData(){
				let res=await this.$api.user_api.getHomeData(); 
				if(res.data.code==200){
					this.num_data={};
					this.num_data=res.data.data;
				}
			},
			
			
			// 获取消息
			async getMessageList(){ 
				let res=await this.$api.user_api.addMsgTypeList();  
				if(res.data.code==200){ 
					this.new_msg_num=0;
					res.data.data.forEach(item=>{
						this.new_msg_num+=Number(item.count)
					})				 	
				}
			}, 
			 
			async getList() {
				let res = await this.$api.project_api.getList({
					page: 1,
					limit: 5,
					status: '-1'
				});    
				if (res.data.code == 200) { 
					this.loadingState.default=false;
					this.pro_list=[];
					res.data.data.list.forEach(item=>{
						item.amount=this.$moneyToFormat(item.amount);
						item.finish_monback=this.$moneyToFormat(item.finish_monback);
					})					
					this.pro_list = res.data.data.list;
				} else {
					this.loading = false;
					uni.showToast({
						icon:'error',
						title:res.data.msg,
						mask:true
					})
				} 				
			},
			
		},
		
		computed:{ 
			list_update(){
				return this.$store.state.msg_state;
			}
		},
		watch:{
			list_update(newer,older){  
				if(newer!=older){
					this.new_msg_num=0; 
					this.getMessageList();
					this.getHomeData();
				}
			} 
		}
	}
</script>

<style lang="less" scoped>
	.body{
		overflow: auto;
	}
	.notice{
		position: absolute;
		right:50rpx;
		top:0;
		color:#000;			
		.num{
			position: absolute;
			left:15rpx;
			top:-15rpx;
			min-width: 18rpx;
			padding:0 8rpx;
			height: 30rpx;
			text-align: center;
			line-height: 30rpx;
			background: #f00;
			border-radius: 38rpx;
			font-size: 26rpx;
			color: #fff;
		}
	}
	.banner{
		position: relative;
		height: 400rpx;
		.setting{
			position: absolute;
			right:50rpx;
			top:0;
			color:#fff;
		}
		
		
		image{
			width: 100%;
		}
	}
	.welcome-text{ 
		display: flex;
		justify-content: space-between;
		margin:20rpx;
		font-size: 30rpx; 		
	}
	.hd-title{
		display: flex;
		justify-content: space-between;
		align-items: center; 
		margin-bottom: 20rpx;
		.title{
			height:30rpx;
			line-height: 30rpx;
			padding-left: 10rpx;
			border-left: 8rpx solid #09f;
			font-size: 32rpx;
			font-weight: bold;
		}
		.more{
			font-size: 28rpx;
			color:#09f;
		}
	}
	.project-info-data{
		display: flex; 
		justify-content: space-between;
		flex-wrap: wrap;
		margin: 0 20rpx;
		.item{
			display: flex; 
			width: 38.5%;
			padding:20rpx 5%;
			margin-bottom: 20rpx; 
			border:1rpx solid #eee;
			border-radius: 15rpx;
			color:#666;
			.icon{
				width: 90rpx; 
				margin-right: 30rpx;
				flex-shrink: 0; 
				image{
					width: 100%;
				}
			}
			.data-info{
				
			}
			.val{
				font-size: 32rpx;
				font-weight: bold;
				color:#09f;
			}
			.text{
				font-size: 30rpx;
			}
		}
		.all{
			background: #fff;
			// background:radial-gradient(circle at left top, #441aee, #521ceb, #6a1ee5);
		}
		.pass{
			background: #fff;
			// background:radial-gradient(circle at left top, #ecc408, #ebb10c, #eba10f);
		}
		.p-1{
			background: #fff;
			// background:radial-gradient(circle at left top, #52b301, #6da241, #52b301);
		}
		.p-2{
			background: #fff;
			// background:radial-gradient(circle at left top, #ecc408, #ebb10c, #eba10f);
		}
		.red{
			background: #fff;
			// background:radial-gradient(circle at left top, #ff0000, #e63737, #ff0000);
		}
	}
	.mod-grid{
		margin: 0 20rpx; 		 
	}
	.project-list{
		.item{
			padding:0 20rpx 20rpx; 
			background: #fff; 
			border-radius: 15rpx;
			.hd{
				padding:15rpx 0;
				border-bottom: 1rpx solid #eee;		
			}
			.date{
				color:#999;
				font-size: 24rpx;
			}
			.title {
				display: flex;
				justify-content: space-between;
				align-items: center;						
				.name{
					max-width: 75%;
					margin-right: 10rpx;
					font-size: 30rpx;
				}
				.status{					
					text{
						border-radius: 5rpx;
						padding:2rpx 5rpx;
					}
				}
				.blue{
					border:1rpx solid #09f;
					color:#09f;
				}
				.red{
					border:1rpx solid #f00;
					color:#f00;
				}
				.grey{
					border:1rpx solid #ccc;
					color:#ccc;
				}
				.orange{
					border:1rpx solid #ff9600;
					color:#ff9600;
				}
				.green{
					border:1rpx solid green;
					color:green;
				}
			}
			.content{
				position: relative;
				padding:20rpx 0 0;
				.finish-icon{
					position: absolute;
					right:0;
					top:0;
					width:180rpx;
					image{
						width: 100%;
					}
				}
				.row-2{
					display: flex;
					justify-content: space-between;
				}
				.s-item{
					display: flex;
					align-items: center;
					margin-bottom: 10rpx;
					font-size: 26rpx;
					color:#666;
					.label{
						width: 140rpx;
						flex-shrink: 0;
					}
					.val{
						display: flex;
						align-items: center;
						flex-grow: 2;
					}
				}
				.progress{
					position: relative;
					height:10rpx;
					width: 60%;
					margin-right: 20rpx;
					border-radius: 15rpx;
					background: #ccc;
					.kd{
						position: absolute;
						left:0;
						top:0;
						height:10rpx;
						border-radius: 15rpx;
						background: #09f;
					}
				}
				.progress-data{
					font-size: 28rpx;
				}				
			}
			.bottom{
				display: flex;
				justify-content: space-between;
				padding:20rpx 0;
				margin-top: 10rpx;
				border-top: 1rpx solid #eee;
				.right{
					display: flex;
					justify-content: flex-end;
					.btn{
						margin-left: 20rpx; 
					}
				}
				.btn{ 
					padding:0 10rpx;
					border-radius: 10rpx;
					font-size: 28rpx;
					color:#fff;
				}
				.blue{
					background:#09f; 
				}
				.red{
					background:#f00; 
				}
				.grey{
					background:#ccc; 
				}
				.orange{
					background: #ff9600;
				}
			}
		}
	} 
	
	
	.task-list{
		padding-top: 20rpx;
		.item{
			border-left: 10rpx solid #09f;
			margin:0 30rpx 20rpx;
			padding:0 20rpx;
			background: #fff;
			border-radius: 15rpx;
			.title{
				display: flex;
				justify-content: space-between;
				border-bottom: 1rpx solid #eee;
				padding:15rpx 0;
				.name{
					font-size: 28rpx;
				}
				.status{
					font-size: 26rpx;
					color:#09f;
				}
			}
			.content{
				display: flex;
				justify-content: space-between;
				padding:30rpx 0;
				.right{
					.user-name{
						text-align: center;
						font-size: 28rpx;
					}
				}
			}
			.date-info{
				display: flex;
				margin-bottom: 20rpx;
				font-size: 28rpx;
				.date{
					margin-right: 20rpx;
				}
				.level{
					color:#f00
				}
			}
			.progress-bar{
				display: flex;
				align-items: center;
				.progress{
					position: relative;
					height:16rpx;
					width: 90%;
					margin-right: 20rpx;
					border-radius: 15rpx;
					background: #ccc;
					.kd{
						position: absolute;
						left:0;
						top:0;
						height:16rpx;
						border-radius: 15rpx;
						background: #09f;
					}
				}
				.progress-data{
					font-size: 28rpx;
				}
			}
			
		}
	} 
</style>
